package com.ding.custommanagersys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.custommanagersys.bo.AreaBo;
import com.ding.custommanagersys.bo.CustomBo;
import com.ding.custommanagersys.entity.Custom;
import com.ding.custommanagersys.entity.Member;
import com.ding.custommanagersys.entity.ResultData;
import com.ding.custommanagersys.service.ICustomService;
import com.ding.custommanagersys.vo.CustomUpdateVo;
import com.ding.custommanagersys.vo.CustomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ding
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/custom")
@CrossOrigin
public class CustomController {

    @Autowired
    private ICustomService customService;

    @RequestMapping("/getAllCustom")
    protected ResultData<List<CustomVo>> listAllCustom(CustomBo bo){
        ResultData<List<CustomVo>> resp = new ResultData<>();
        Page<CustomVo> allCustom = customService.getAllCustom(bo);
        resp.setCount(allCustom.getTotal());
        resp.setData(allCustom.getRecords());
        return resp.ok();
    }

    /**
     * 添加 客户信息
     */
    @PostMapping("/addCustom")
    public ResultData<Integer> addCustom(@RequestBody CustomVo customVo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = customService.addCustom(customVo);
        if(num == 0){
            resp.error("添加失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

    /**
     * 删除 客户
     */
    @PostMapping("/deleteCustom")
    public ResultData<Integer> deleteCustom(@RequestBody CustomBo bo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = customService.deleteCustom(bo.getIds());
        if(num != 0){
            resp.ok();
        }else{
            resp.error("该编号下没有客户信息，或编号为空");
        }
        return resp;
    }

    /**
     * 修改 客户
     */
    @PostMapping("/updateCustom")
    public ResultData<Integer> updateCustom(CustomUpdateVo customUpdateVo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = customService.updateCustom(customUpdateVo);
        if(num == 0){
            resp.error("修改失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }




}
